using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public class AnimationSwitchHelper : NodeHelper
    {
        public bool restore;
        private AnimationHelper _AnimationHelper;
        private AnimationManagerHelper _AnimationManagerHelper;
        private bool open;

        private AnimationHelper AnimationHelper
        {
            get
            {
                if (_AnimationHelper == null) _AnimationHelper = GetComponent<AnimationHelper>();
                return _AnimationHelper;
            }
        }

        private AnimationManagerHelper AnimationManagerHelper
        {
            get
            {
                if (_AnimationManagerHelper == null) _AnimationManagerHelper = GetComponent<AnimationManagerHelper>();
                return _AnimationManagerHelper;
            }
        }

        protected override BindDataType bindDataType => BindDataType.Bool;

        protected override void BindData(bool b)
        {
            if (!open && b) Show();
            if (open && !b) Hide();
            open = b;
        }

        void Show()
        {
            if (AnimationManagerHelper != null)
                AnimationManagerHelper.Play();
            else if (AnimationHelper != null) AnimationHelper.Play();
        }

        void Hide()
        {
            if (restore)
            {
                if (AnimationManagerHelper != null)
                    AnimationManagerHelper.Restore();
                else if (AnimationHelper != null) AnimationHelper.Restore();
            }
            else
            {
                if (AnimationManagerHelper != null)
                    AnimationManagerHelper.Backward();
                else if (AnimationHelper != null) AnimationHelper.Backward();
            }
        }
    }
}